package com.wangxiang.sys.manage.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangxiang.common.utils.*;
import com.wangxiang.sys.manage.dao.*;
import com.wangxiang.sys.manage.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wangxiang.sys.manage.service.AuditLogService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;


@Service("auditLogService")
public class AuditLogServiceImpl extends ServiceImpl<AuditLogDao, AuditLogEntity> implements AuditLogService {
    @Autowired
    private AuditLogDao auditLogDao;
    @Autowired
    private AuditDao auditDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleUserDao roleUserDao;
    @Autowired
    private RoleDao roleDao;

    @Override
    public int passInsert(HashMap<String, Object> map) {
        AuditLogEntity auditLogEntity = new AuditLogEntity();
        int orderId = Integer.valueOf(map.get("orderId").toString());
        int auditId = Integer.valueOf(map.get("auditId").toString());
        AuditEntity auditEntity = auditDao.selectById(orderId);
        auditLogEntity.setAuditTime(new Date());
        auditLogEntity.setStatus(0);
        auditLogEntity.setApplyType(auditEntity.getStatus());
        auditLogEntity.setAuditUserId(auditId);
        auditLogEntity.setApplyTime(auditEntity.getTime());
        auditLogEntity.setApplyUserId(auditEntity.getUserId());
        auditLogEntity.setOrderId(orderId);
        //更新审核表
        int i = auditDao.updateStatus(auditEntity.getId(), 2);
        if (i>0){
            //插入日志表
            int insert = auditLogDao.insert(auditLogEntity);
            if (insert>0){
                return auditEntity.getSpuId();
            }
        }
        return 0;


    }

    @Override
    public PageEntity page(HashMap<String, Integer> map) {
        PageHelper.startPage(map.get("pageNum"),map.get("pageSize"));
        List<AuditLogEntity> auditLogEntities = auditLogDao.selectList(null);
        auditLogEntities.forEach((item)->{
            TimeUtils timeUtils = new TimeUtils();
            String applyTime = timeUtils.timeChange(item.getApplyTime());
            String auditTime = timeUtils.timeChange(item.getAuditTime());
            item.setStrAuditTime(auditTime);
            item.setStrApplyTiem(applyTime);

        });
        PageInfo<AuditLogEntity> auditLogEntityPageInfo = new PageInfo<>(auditLogEntities);
        PageGenerate pageGenerate = new PageGenerate();
        PageEntity generate = pageGenerate.generate(auditLogEntityPageInfo);
        return generate;
    }

    @Override
    public List<HashMap<String, Object>> pageList(List<AuditLogEntity> list) {
        ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
        list.forEach((item)->{
            HashMap<String, Object> map = new HashMap<>();

            AuditEntity auditEntity = auditDao.selectById(item.getOrderId());
            UserEntity auditUser = userDao.selectById(item.getAuditUserId());
            UserEntity applyUser = userDao.selectById(item.getApplyUserId());
            //关系表
            int applyRoleId = roleUserDao.getRoleIdByUser(item.getApplyUserId());
            int auditRoleId = roleUserDao.selectRoleId(item.getAuditUserId());
            //角色表
            RoleEntity applyRoleEntity = roleDao.selectById(applyRoleId);
            RoleEntity auditRoleEntity = roleDao.selectById(auditRoleId);
            map.put("orderId",item.getOrderId());
            //申请人，申请时间，申请；类型
            map.put("applyName",applyUser.getLoginId());
            map.put("applyRole",applyRoleEntity.getName());
            map.put("applyUserId",applyUser.getUserId());
            map.put("applyRoleId",applyRoleId);
            map.put("applyTime",item.getStrApplyTiem());

            //审核人，审核时间,审核状态
            map.put("auditName",auditUser.getLoginId());
            map.put("auditRole" , auditRoleEntity.getName());
            map.put("auditUserId" , auditUser.getUserId());
            map.put("auditRoleId", auditRoleEntity.getId());
            map.put("auditType" , item.getStatus());
            map.put("auditTime" , item.getStrAuditTime());
            if (item.getRemark()!=null){
                map.put("remark",item.getRemark());
            }
            map.put("applyType",item.getApplyType());

            //商品信息spuId
            map.put("spuId",auditEntity.getSpuId());
            hashMaps.add(map);
        });
        return hashMaps;
    }

    @Override
    public int invalidShop(int orderId, int userId, String remark) {

        AuditEntity auditEntity = auditDao.selectById(orderId);
        AuditLogEntity auditLogEntity = new AuditLogEntity();
        //插入日志表
        auditLogEntity.setOrderId(orderId);
        auditLogEntity.setApplyUserId(auditEntity.getUserId());
        auditLogEntity.setAuditUserId(userId);
        auditLogEntity.setApplyTime(auditEntity.getTime());
        auditLogEntity.setAuditTime(new Date());
        auditLogEntity.setStatus(1);
        auditLogEntity.setRemark(remark);
        auditLogEntity.setApplyType(auditEntity.getStatus());

        //插入日志表
        int insert = auditLogDao.insert(auditLogEntity);
        if (insert>0){
            //跟新审核表状态
            int i = auditDao.updateStatus(orderId, 2);
            if (i>0){
                return auditEntity.getSpuId();
            }
        }
        return 0;
    }


}